package cn.chen.thread.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {

    public static void main(String[] args) {
        MyService service = new MyService();
        for(int i = 0; i < 5; i ++){
            new MyThread(service).start();
        }
    }

}

class MyService{
    private Lock lock = new ReentrantLock();

    public void testMethod(){
        lock.lock();
        for(int i = 0;i < 5;i ++){
            System.out.println("ThreadName="+Thread.currentThread().getName()+" "+(i + 1));
        }
        lock.unlock();
    }
}

class MyThread extends Thread{

    private MyService myService;

    public MyThread(MyService myService){
        super();
        this.myService = myService;
    }

    @Override
    public void run() {
        myService.testMethod();
    }
}